// import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';

// import operate from '@feidao/web/atom/nodejs/db/operate';
// import sys_date from '@feidao/web/atom/nodejs/msg/sys-date'; // 获取系统时间
// import { IBaseTable } from '@dfeidao/service/interfaces';
// import { systemid } from './config';
interface Message {
	filter: {
		tenantid: string;
		fujd: [];
		zjd: [];
		client_type: string;
	};
	asc: string[];
	desc: string[];
	// systemid: string,
	page_size: number;
	page_no: number;
}
interface D {
	formid: string;
}
// 保存服务
export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	const { del, exec } = operate(action_id, session_id, systemid);
	del('pub_form', { productid: msg.filter.tenantid, client_type: msg.filter.client_type });
	const res = await exec();
	if (res.code === 1) {
		const { exec: a, prepare } = query(action_id, session_id, systemid);
		prepare('pub_form', ['_id', 'formid'], { productid: msg.filter.tenantid, client_type: msg.filter.client_type }, 200, 1, [], []);
		const [formdata] = await a<D>();
		const fidarr = new Array();
		formdata.forEach((v) => {
			fidarr.push(v.formid);
		});
		const { del: d1, exec: e1 } = operate(action_id, session_id, systemid);
		d1('pub_form_code', { productid: msg.filter.tenantid, formid: { $in: fidarr } });
		const res1 = await e1();
		if (res1.code === 1) {
			const { insert: in1, exec: e2 } = operate(action_id, session_id, systemid);
			msg.filter.fujd.forEach((i) => {
				in1('pub_form', i);
			});
			const res2 = await e2();
			if (res2.code === 1) {
				const { insert: in2, exec: e3 } = operate(action_id, session_id, systemid);
				msg.filter.zjd.forEach((i) => {
					in2('pub_form_code', i);
				});
				const res3 = await e3();
				if (res3.code === 1) {
					return 'success';
				} else {
					return 'fail';
				}
			} else {
				return 'fail';
			}
		} else {
			return 'fail';
		}
	} else {
		return 'fail';
	}
}
